const TBODY1 = [
    ['获取之后','天没有跟进'],
    ['距离上次跟进','天未成交（新建成商机）'],
    ['已成交（新建成商机）的客户','天掉入客户公海池'],
]
const TBODY2 = [
    ['退回之后前负责人','天无法再次领取'],
]
let receiveData = {} // 线索领取规则接口返回值
let recoveryData = {} // 线索回收规则接口返回值
const _DlgAddRules = new DlgAddRules()
// 编辑规则
function handleRuleEdit(row){
    DlgAddRules.create({title:'编辑规则', row:row, ruleId:row.ruleId, defaultValue: row.label,value:row.value })
}

// 删除规则
function handleRuleDelete(ruleId){
    if (!ruleId) return
    const deleteAfter =  function() {
        _DlgAddRules.getRulesList()
    }
    renderRbcomp(<RulesDeleteConfirm ids={ruleId} deleteAfter={deleteAfter} message="确定删除吗？" />)
}

/*查询线索领取规则*/

function getReceiveRule(){
    $.ajax({
        url:"/limitRule/selectReceiveRule/XnAccount",
        type:"GET",
        success:(res)=>{
            if(res.data.code === 200){
                receiveData = res.data.data
                const n = receiveData.label === 'week' ? 0 : 1
                $('#receiveRulesRadioGroup').find('.custom-radio').eq(n).children('input').prop('checked',true)
                $('#qLimit').val(receiveData.value)
            }
        }
    })
}
/*线索领取规则 表单*/
function receiveSubmit(){
    $.ajax({
        url:'/limitRule/updateReceiveRule',
        type:"POST",
        headers:{
            'Content-Type':  "application/json;charset=UTF-8",
        },
        data:JSON.stringify(receiveData),
        success:function(res){
            getReceiveRule()
            RbHighbar.success('修改成功')
        }
    })
}
// 查询线索回收详情
function getRecoveryInfo(){
    $.ajax({
        url:'/limitRule/selectFallbackRule/XnAccount',
        type:"GET",
        success: (res)=>{
            if(res.data.code === 200){
                recoveryData = res.data.data
                renderTbody(recoveryData.list, 'tbodyRule1',TBODY1)
                renderTbody(recoveryData.againRule, 'tbodyRule2',TBODY2)
                if(recoveryData.lockRule){
                    $('#lockRule').val(recoveryData.lockRule)
                }
                const flag = recoveryData.isDisable === 0 ? true : false
                $('#openAuthRecovery').find('input[type=checkbox]').prop('checked',flag)
            }
        }
    })
}
// 渲染tbody
function renderTbody(arr,el,arr2){
    let html = ``
    for(let i = 0; i < arr.length; i++){
        const item = arr[i]
        const itemStr = JSON.stringify(item)
        html += `
                        <tr>
                            <td>${item.label}</td>
                            <td>
                                <div class="input-group input-group-sm">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text">${arr2[i][0]}</span>
                                    </div>
                                    <input type="number" data-label="${item.label}" onblur='handleBlurBlur(${itemStr},this)' data-el="${el}" data-idx="${i}" id="${el}_input_${i}" value="${item.value}" class="form-control" placeholder="" />
                                    <div class="input-group-append">
                                        <span class="input-group-text">${arr2[i][1]}</span>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    `
    }
    $(`#${el}`).html(html)
}
// 线索回收规则 失去焦点事件
function handleBlurBlur(row,el){
    const reg = /^\+?[1-9][0-9]*$/
    const _val = $(el).val()
    if(!reg.test(_val)){
        RbHighbar.error('请填写大于0的数字')
        return
    }
    recoverySubmit(row,_val)
}
// 线索回收规则 表单
function recoverySubmit({entity,isDisable,label,ruleCode,ruleId,ruleName,lockRule},value){
    const data = {
        entity,
        isDisable,
        label,
        ruleCode,
        ruleId,
        ruleName,
        lockRule,
        value
    }
    $.ajax({
        url:"/limitRule/updateFallbackRule",
        type:"POST",
        data: JSON.stringify(data),
        success:()=>{
            RbHighbar.success('修改成功')
        }
    })
}
// 客户锁
function handleBlurBlurLock(el){
    const reg = /^\+?[1-9][0-9]*$/
    const _val = $(el).val()
    if(!reg.test(_val)){
        RbHighbar.error('请填写大于0的数字')
        return
    }
    $.ajax({
        type:"GET",
        url:`/limitRule/updateAccountLockRule?value=${_val}`,
        success:(res)=>{
            RbHighbar.success('修改成功')
        }
    })
    // recoverySubmit(recoveryData,_val)
}
$(document).ready(function(){
    // 监听切换选项卡
    $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
        switch (( e.target.toString().split("#")[1])){
            case 'selectLeadTeamForAdmin':
                break;
            case 'limit':
                _DlgAddRules.getRulesList()
                break;

            case 'receiveRules':
                // 查询线索领取规则
                getReceiveRule()
                break;
            case 'recoveryRules':
                getRecoveryInfo()
                break;
        }

    })
    // 切换领取数量限制
    $(document).on('click','#checkboxLimit',function(){
        $.ajax({
            url:`/limitRule/isDisabledCountRule/XnAccount`,
            type:"GET",
            success:(res)=>{
                RbHighbar.success('切换成功')
            }
        })
    })
    // 添加规则-创建弹窗
    $('#addRules').click(function () {
        DlgAddRules.create({entity:__PageConfig.entity[0]})
    })

    /*****线索领取规则*****/
    // 切换 选择周期
    $(document).on('change','input[name=cycle]',function(){
        const _val = $(this).val()
        receiveData.label = _val
        receiveSubmit()
    })
    // 线索领取规则-数量限制
    $(document).on('blur','#qLimit',function(){
        const _val = $(this).val()
        receiveData.value = _val
        receiveSubmit()
    })

    /**********线索回收规则***********/

    // 勾选 自动回收
    $('#openAuthRecovery').click(function(){
        $.ajax({
            url:'/limitRule/isDisabledFallback/XnAccount',
            type:"GET",
            success: (res)=>{
                RbHighbar.success('切换成功')
                getRecoveryInfo()
            }
        })
    })
})